package cn.edu.sdjzu.xg.bysj.service;

import cn.edu.sdjzu.xg.bysj.dao.GraduateProjectDao;
import cn.edu.sdjzu.xg.bysj.domain.GraduateProject;
import exception.BysjException;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public final class GraduateProjectService {
    private static GraduateProjectDao graduateProjectDao = GraduateProjectDao.getInstance();
    private static GraduateProjectService graduateProjectService = new GraduateProjectService();
    private static Connection connection;

    {
        try {
            connection = JdbcHelper.getConn();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static GraduateProjectService getInstance() {
        return graduateProjectService;
    }

    public Collection<GraduateProject> findAll(
            List<Condition> conditionList_json_str,
            Pagination pagination)
            throws SQLException {

        Connection connection = JdbcHelper.getConn();
        Collection<GraduateProject> graduateProjects =
                graduateProjectDao.findAll(conditionList_json_str, pagination, connection);
        JdbcHelper.close(connection);
        return graduateProjects;
    }


    public GraduateProject find(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        GraduateProject result = graduateProjectDao.find(id, connection);
        JdbcHelper.close(connection);
        return result;
    }

    public boolean update(GraduateProject graduateProject) throws SQLException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        boolean updated = false;
        //将自动提交设置为false
        connection.setAutoCommit(false);
        try {
            updated = graduateProjectDao.update(graduateProject, connection);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设置为ture
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return updated;
    }

    public boolean add(GraduateProject graduateProject) throws SQLException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        boolean added = false;
        //将自动提交设置为false
        connection.setAutoCommit(false);
        try {
            added = graduateProjectDao.add(graduateProject, connection);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设置为ture
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return added;
    }

    public boolean updateScratch(GraduateProject graduateProject) throws SQLException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        boolean updated = false;
        //将自动提交设置为false
        connection.setAutoCommit(false);
        try {
            updated = graduateProjectDao.updateScratch(graduateProject, connection);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设置为ture
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return updated;
    }

    public boolean addScratch(GraduateProject graduateProject) throws SQLException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        boolean added = false;
        //将自动提交设置为false
        connection.setAutoCommit(false);
        try {
            added = graduateProjectDao.addScratch(graduateProject, connection);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设置为ture
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return added;
    }

    public boolean delete(Integer id) throws SQLException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        boolean deleted = false;
        //将自动提交设置为false
        connection.setAutoCommit(false);
        try {
            deleted = graduateProjectDao.delete(id, connection);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设置为ture
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return deleted;
    }

    public boolean delete(List<GraduateProject> List) throws SQLException, BysjException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        boolean deleted = false;
        //将自动提交设置为false
        connection.setAutoCommit(false);
        try {
            deleted = graduateProjectDao.delete(List, connection);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设置为ture
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return deleted;
    }
}